import 'dart:io';

import 'package:cjybase/base/base_page_state.dart';
import 'package:cjybase/network/net.dart';
import 'package:cjybase/util/file_util.dart';
import 'package:cjybase/util/string_util.dart';
import 'package:cjybase/util/time_util.dart';
import 'package:cjybase/widget/dialog_factory.dart';
import 'package:cjybase/util/permission/permission_util.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../page_manage.dart';

abstract class MainPageState extends BasePageState<MainPage> {
  String videoUrl = "https://media.w3.org/2010/05/sintel/trailer.mp4";

  list() {
    push(context, ListPage());
  }

  network() {
    push(context, NetworkPage());
  }

  oneDialog() {
    DialogFactory.contentBtn1Dialog(
        context, "啊哈哈是", "dsjaljdlajfdlsaj第六届案例多少斤啊来得及都是垃圾的拉进来的大萨达大大萨达", () {
      "我知道了".log();
    });
  }

  twoDialog() {
    DialogFactory.contentBtn2Dialog(
        context, "啊哈哈是", "dsjaljdlajfdlsaj第六届案例多少斤啊来得及都是垃圾的12312312拉进来的大萨达大大萨达",
        () {
      "取消".log();
    }, () {
      "确认".log();
    });
  }

  getPath() async {
    "${await FileUtil.getCacheDir()}".log();
    "${await FileUtil.getFilesDir()}".log();
  }

  permission() async {
    "${await PermissionUtil.request(context, Permission.photosAddOnly)}".log();
  }

  download() async{
    // 权限获取
    bool isPermission = await PermissionUtil.request(context,
        Platform.isIOS ? Permission.photosAddOnly : Permission.storage);
    if (isPermission) {
      String tempDir = await FileUtil.getCacheDir();
      String fileName = "${TimeUtil.nowTimeMilliseconds()}.mp4";
      EasyLoading.show(status: '视频下载连接中。。。');
      // 下载
      Net.download(videoUrl, "$tempDir/$fileName", progress: (String progress) {
        double progressUI = double.parse(progress.replaceAll("%", ""));
        EasyLoading.showProgress(progressUI / 100,
            status: progress, maskType: EasyLoadingMaskType.black);
        print(progress);
      }, finish: (String savePath) {
        // 保存到相册
        FileUtil.saveImgOrVideoToGallery(savePath, isDelSourceFile: true);
        EasyLoading.dismiss();
        "视频已保存到相册".toast();
      }, error: (String error) {
        EasyLoading.dismiss();
      });
    } else {
      "没有权限".toast();
    }
  }
}
